using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimatedSprite
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D _explostionTexture;

        List<SimpleAnimatedSprite> _animatedSprites;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            _animatedSprites = new List<SimpleAnimatedSprite>();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            _explostionTexture = Content.Load<Texture2D>("explosions");

            for (int i = 0; i < 8; i++)
            {
                SimpleAnimatedSprite animatedSprite = new SimpleAnimatedSprite(_explostionTexture, TimeSpan.FromMilliseconds(30),
                                                                   new Vector2(0, i*64), new Vector2(64), 16,16);
                
                animatedSprite.Position = new Vector2(i*64,0);
                animatedSprite.PlayAnimation(true);
                //_animatedSprites.Add(animatedSprite);
               
            }

            Texture2D walkingTexture = Content.Load<Texture2D>("walksequence_spritesheet");
            SimpleAnimatedSprite animatedSpriteWalking = new SimpleAnimatedSprite(walkingTexture, TimeSpan.FromMilliseconds(150), new Vector2(0, 0), new Vector2(240,295), 30, 6)
            {
                Position = new Vector2(0, 80)
            };
            animatedSpriteWalking.PlayAnimation(true);
            _animatedSprites.Add(animatedSpriteWalking);


            Texture2D ringsTexture = Content.Load<Texture2D>("spritesheet");
            SimpleAnimatedSprite animatedSpriteRings = new SimpleAnimatedSprite(ringsTexture, TimeSpan.FromMilliseconds(100), new Vector2(0, 0), new Vector2(60, 60), 60, 8)
            {
                Position = new Vector2(400, 80)
            };
            animatedSpriteRings.PlayAnimation(true);

            //_animatedSprites.Add(animatedSpriteRings);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            foreach (SimpleAnimatedSprite animatedSprite in _animatedSprites)
            {
                animatedSprite.Update(gameTime);
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Blue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            foreach (SimpleAnimatedSprite animatedSprite in _animatedSprites)
            {
                animatedSprite.Draw(gameTime, spriteBatch);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
